// nameAuthentication.js
var app = getApp();
import camera from '../../utils/camera.js';
import { showTi } from "../../utils/modal.js"
Page({
  /**
   * 页面的初始数据
   */
  data: {
    remote_headimg_url: '',
    real_name: '',
    uuid: '',
    pictures: [],
    loading: false,
    user_type:null,
    user_typeName:null
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.data.pictures = []
    this.choiceDatas()
  },
  onUnload: function () {
    wx.removeStorageSync('choiceType')
  },
  bindHandlerChange(e) {
    let type = e.currentTarget.dataset.type;
    let obj = {};
    obj[type] = e.detail.value;
    this.setData(obj);
  },
  //头像
  camera: function (e) {
    let self = this;
    let type = e.currentTarget.dataset.type;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        console.log(res)
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        let obj = {};
        if (type == 'remote_headimg_url') {
          obj[type] = res.tempFilePaths;
        } else {
          // card
          let pictures = self.data.pictures;
          pictures[parseInt(type)] = res.tempFilePaths[0];
          obj.pictures = pictures;
        }
        self.setData(obj);
      }
    });
  },
  //提交
  authButton: function () {
    let self = this
    let params = self.data;
    let picturesList = []
    let picturesObj
    let regx = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
    if (!(self.data.uuid) || !(self.data.real_name)) {
      showTi("身份证号或姓名不能为空")
    } else if (!regx.test(self.data.uuid)) {
      showTi("请输入正确的身份证号码")
    } else {
      console.log(self.data.pictures)
      let imgs_arr = self.data.pictures.concat(self.data.remote_headimg_url);
      camera.uploadFile(imgs_arr, { type: { "type": 'card' } }).then(res => {
        console.log(res.length)
        let imgs = res;
        if (imgs.length < 3) {
          showTi("请上传头像或身份证照片")
        }
        params.remote_headimg_url = imgs[2].remote_image_url;
        delete params.pictures[2];
        params.pictures = imgs;
        self.setData({ loading: true })
        app.query('web/users/service_info', params, 'post').then(res => {
          if (res.code == 200) {
            self.setData({ loading: false })
            console.log(res);
            wx.navigateTo({ url: '/pages/authentication/authentication' })
            app.globalData.userInfo = res.user
          } else {
            console.log(res)
            wx.navigateTo({ url: '/pages/fail/fail?msg=' + res.msg })
            self.setData({ loading: false })
          }
          params = self.data;
        })
      })
    }
  },
  //数据处理
  choiceDatas:function(){
    this.setData({
      user_type: wx.getStorageSync('choiceType'),
    })
    console.log(wx.getStorageSync('choiceType'))
    if (this.data.user_type=='driver'){
      this.setData({
        user_typeName: "我是司机"
      })
    }else{
      this.setData({
        user_typeName:"我是师傅"
      })
    }
  }
})